package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.jzo2o.api.customer.InstitutionApi;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.api.publics.dto.response.BooleanResDTO;
import com.jzo2o.common.constants.CommonStatusConstants;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.utils.IdUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.model.dto.request.InstitutionResetPasswordReqDTO;
import com.jzo2o.customer.service.IRegisterService;

import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.IServeProviderSettingsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class IRegisterServiceImpl implements IRegisterService {

    @Resource
    private SmsCodeApi smsCodeApi;
    @Resource
    private IServeProviderService serveProviderService;
    @Resource
    IServeProviderSettingsService serveProviderSettingsService;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(InstitutionRegisterReqDTO institutionRegisterReqDTO) {
        if (ObjectUtils.isEmpty(institutionRegisterReqDTO)) {
            throw new CommonException("请求体为空！");
        }
        // 验证码校验
        if (StringUtils.isEmpty(institutionRegisterReqDTO.getVerifyCode())){
            throw new BadRequestException("验证码错误，请重新获取");
        }
        boolean verify = smsCodeApi.verify(institutionRegisterReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, institutionRegisterReqDTO.getVerifyCode()).getIsSuccess();
        if (!verify){
            throw new BadRequestException("验证码错误,请重新输入！");
        }

        // 注册
        // 校验手机号是否存在
        ServeProvider existServeProvider = serveProviderService.lambdaQuery().eq(ServeProvider::getPhone, institutionRegisterReqDTO.getPhone()).one();
        if (existServeProvider != null) {
            if(existServeProvider.getType().equals(UserType.WORKER)){
                throw new BadRequestException("该账号已被服务人员注册");
            }else {
                throw new BadRequestException("该账号已被机构注册");
            }
        }
        Integer type = 3;
        String password = bCryptPasswordEncoder.encode(institutionRegisterReqDTO.getPassword());
        //新增服务人员/机构信息
        ServeProvider serveProvider = new ServeProvider();
        serveProvider.setPhone(institutionRegisterReqDTO.getPhone());
        serveProvider.setPassword(password);
        serveProvider.setType(type);
        serveProvider.setStatus(CommonStatusConstants.USER_STATUS_NORMAL);
        serveProvider.setCode(IdUtils.getSnowflakeNextIdStr());
        serveProviderService.save(serveProvider);
        //新增服务人员/机构配置信息同步表信息,方便后期对配置项进行配置
        serveProviderSettingsService.add(serveProvider.getId(), type);

    }


}
